package com.zwk.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwk.base.entity.CommonResult;
import com.zwk.edu.entity.Teacher;
import com.zwk.edu.entity.vo.TeacherQuery;
import com.zwk.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zwk
 * @since 2022-08-17
 */
@Api("讲师管理")
@RestController
@CrossOrigin
@RequestMapping("/edu/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    // 查询讲师表的数据
    @ApiOperation(value = "查询讲师表的所有数据")
    @GetMapping("/findAll")
    public CommonResult findAllTeacher(){

        List<Teacher> list = teacherService.list(null);
        return CommonResult.ok().data("list", list);

    }

    // 逻辑删除
    @ApiOperation(value = "逻辑删除")
    @DeleteMapping("/delete/{id}")
    public CommonResult removeTeacher(@PathVariable("id") String id){

        boolean result = teacherService.removeById(id);
        if (result){
            return CommonResult.ok();
        } else {
            return CommonResult.error();
        }

    }

    // 分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("/page/{current}")
    public CommonResult getPageList(@PathVariable("current") Integer current){
        //创建page对象
        Page<Teacher> pageTeacher = new Page<>(current,5);
        IPage<Teacher> page = teacherService.page(pageTeacher, null);

        // 判断current是否合法
        if(current > page.getPages()){
            current = Math.toIntExact(page.getPages());
            pageTeacher = new Page<>(current,5);
            page = teacherService.page(pageTeacher, null);
        }

        long total = page.getTotal();
        List<Teacher> records = page.getRecords();

        return CommonResult.ok().data("total", total).data("records", records);

    }

    // 条件查询
    @ApiOperation(value = "条件查询")
    @PostMapping("/pageTeacherCondition/{current}")
    public CommonResult pageTeacherCondition(@PathVariable("current") Integer current,
                                             @RequestBody(required = false) TeacherQuery teacherQuery){

        Page<Teacher> pageTeacher = new Page<>(current,5);

        // 条件构造器
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(teacherQuery.getName())){
            queryWrapper.like("name", teacherQuery.getName());
        }
        if (!ObjectUtils.isEmpty(teacherQuery.getLevel())){
            queryWrapper.eq("level", teacherQuery.getLevel());
        }
        if (!ObjectUtils.isEmpty(teacherQuery.getBegin())){
            queryWrapper.ge("gmt_create", teacherQuery.getBegin());
        }
        if (!ObjectUtils.isEmpty(teacherQuery.getEnd())){
            queryWrapper.le("gmt_modified", teacherQuery.getEnd());
        }

        // 排序
        queryWrapper.orderByDesc("gmt_create");

        IPage<Teacher> page = teacherService.page(pageTeacher, queryWrapper);

        // 判断current是否合法
        if(current > page.getPages()){
            current = Math.toIntExact(page.getPages());
            pageTeacher = new Page<>(current,5);
            page = teacherService.page(pageTeacher, queryWrapper);
        }

        long total = page.getTotal();
        List<Teacher> records = page.getRecords();

        return CommonResult.ok().data("total", total).data("records", records);

    }

    // 添加讲师
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public CommonResult addTeacher(@RequestBody Teacher teacher){

        // 设置默认头像
        if (teacher.getAvatar() == null){
            teacher.setAvatar("https://zwk-guli.oss-cn-hangzhou.aliyuncs.com/01.jpg");
        }

        boolean result = teacherService.save(teacher);
        if (result){
            return CommonResult.ok();
        } else {
            return CommonResult.error();
        }

    }

    // 根据id查询讲师
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("/getTeacher/{id}")
    public CommonResult getTeacherById(@PathVariable("id") String id){
        Teacher teacher = teacherService.getById(id);
        return CommonResult.ok().data("teacher", teacher);
    }

    // 修改讲师数据
    @ApiOperation(value = "修改讲师数据")
    @PostMapping("/update")
    public CommonResult updateTeacher(@RequestBody Teacher teacher){

        // TODO name不能重复，需要判断 ....

        boolean result = teacherService.updateById(teacher);
        if (result){
            return CommonResult.ok();
        } else {
            return CommonResult.error();
        }
    }

}

